<?php
/**
 * Created by PhpStorm.
 * User: 17208
 * Date: 2019/6/17
 * Time: 10:05
 */

namespace app\api\controller;

use app\common\lib\Export;
use \think\Exception;
use app\common\lib\Request;
use app\api\model\User;
use app\common\facade\WebSocketToken;
use app\server\model\Chat_record;
use app\api\model\Team_team;
use app\api\model\Team_user;

class Chat extends Base
{
    private $cmd_data = [
        "100"=>"chat_send_single",   //发送个人消息
        "101"=>"chat_send_team",   //发送群体消息
    ];
    public function index(){
        try{
            // 检验登录情况
            parent::check_user_info();

            $cmd = Request::get('cmd');
            if(!array_key_exists($cmd,$this->cmd_data)) throw new Exception("参数错误");
            $method = $this->cmd_data[$cmd];

            $this->$method();
        }catch (Exception $e){
            Export::exp_err($e->getMessage());
        }

        return '';
    }
    /** 100 发送个人消息 */
    private function chat_send_single(){
        $data = Request::post(['user_id',"message"]);

        $user_id = $data['user_id'];

        $user = User::get($user_id);
        if(!$user) throw new Exception("用户不存在");

        // 对方存在则发送
        $send_fd = WebSocketToken::get_fd($user_id);
        if(WebSocketToken::exist($send_fd)){
            $data = [
                "message"=>$data['message'],
                "user"=>[
                    "id"=>$user_id,
                    "name"=>$user->name,
                    "pic"=>$user->pic_url,
                ],
            ];
            Export::socket_exp_ok($send_fd,Export::$socket_single_chat_cmd,["data"=>$data]);
            $is_receive = 1;
        }else{
            $is_receive = 0;
        }

        Chat_record::create([
            "user_id"=>$user_id,
            "send_id"=>$this->user_id,
            "send_content"=>$data['message'],
            "is_receive"=>$is_receive
        ]);

        Export::exp_ok();
    }
    /** 101 发送消息至团队 */
    public function chat_send_team(){
        $data = Request::post(['team_id',"message"]);

        $team_id = $data['team_id'];
        $message = $data['message'];

        $team = Team_team::get($data['team_id']);
        if(!$team) throw new Exception("聊天组不存在");

        $team_user = Team_user::where(["team_id"=>$team_id])->select()->toArray();
        $user_ids = array_column($team_user,"user_id");
        if(!in_array($this->user_id,$user_ids)) throw new Exception("您不在该聊天组！");

        $record_list = [];
        foreach ($team_user as $user){
            $re_user = User::get($user['user_id']);
            if(!$re_user) continue;
            // 检验用户是否在线
            $fd = WebSocketToken::get_fd($re_user['id']);
            $is_exist = WebSocketToken::exist($fd);
            if($is_exist){
                $data = [
                    "message"=>$data['message'],
                    "user"=>[
                        "id"=>$re_user['id'],
                        "name"=>$re_user['name'],
                        "pic"=>$re_user['pic_url'],
                    ],
                    "team"=>[
                        "id"=>$team_id,
                        "name"=>$team['name']
                    ],
                ];
                Export::socket_exp_ok($fd,Export::$socket_team_chat_cmd,["data"=>$data]);
                $is_receive = 1;
            }else{
                $is_receive = 0;
            }
            $record_list[] = [
                "user_id"=>$re_user['id'],
                "send_id"=>$this->user_id,
                "team_id"=>$team_id,
                "send_content"=>$message,
                "is_receive"=>$is_receive,
            ];
        }
        // 存入数据库
        $chat_record = new Chat_record();
        $chat_record->saveAll($record_list);

        Export::exp_ok();
    }
}